﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class Main_Accounts_VoucherFormatEdit : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = string.Empty;    
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            BindParentVoucher();
            BindVoucherFormat(Request.QueryString["typeid"].ToString());
        }
    }
    protected void BindParentVoucher()
    {
        strsql = "SELECT TYPEID,PARENTTYPEID,TRANSACTIONNAME FROM ACC_TRANSETTINGS WHERE TYPEID=PARENTTYPEID AND TYPEID<>0";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            ddlParentVoucher.DataSource = DT;
            ddlParentVoucher.DataTextField = "TRANSACTIONNAME";
            ddlParentVoucher.DataValueField = "TYPEID";
            ddlParentVoucher.DataBind();
            ddlParentVoucher.Items.Insert(0, new ListItem("-Select Parent-", "-1"));
        }
    }
    protected void BindVoucherFormat(string typeid)
    {
        strsql= "select * from ACC_TranSettings where Typeid="+typeid;
        DataTable dt = DU.getdata(strsql);
        if (dt.Rows.Count > 0)
        {
            txtVoucherName.Text = dt.Rows[0]["TRANSACTIONNAME"].ToString();
            txtAbbreviation.Text = dt.Rows[0]["ABBREVIATION"].ToString();
            txtEffectiveDate.Text = dt.Rows[0]["EFFECTIVEDATE"].ToString();
            if (dt.Rows[0]["EFFECTIVEDATE"].ToString() == "")
            {
                txtEffectiveDate.Text = string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(System.DateTime.Now)).ToString();
            }
            string parenttypeid = dt.Rows[0]["PARENTTYPEID"].ToString();
            ddlParentVoucher.SelectedValue = parenttypeid;
        }
        string sql = "select * from ACC_TRANSETTINGDETAILS where typeid =" + typeid;
        DataTable dt1 = DU.getdata(sql);
        if (dt1.Rows.Count > 0)
        {
            txtPrefix.Text = dt1.Rows[0]["PREFIX"].ToString();
            txtSuffix.Text = dt1.Rows[0]["SUFFIX"].ToString();
            txtStartNumber.Text = dt1.Rows[0]["STARTNO"].ToString();
            txtCommonNarration.Text = dt1.Rows[0]["NARRATION"].ToString();
            //bool print = dt1.Rows[0]["@PRINTONSAVE"].ToString();
            //chkPrintAfterSave.Checked = print;
        }
    }
  
    protected bool Validate()
    {
        bool flag = true;
        strsql = "select TRANSACTIONNAME FROM ACC_TRANSETTINGS WHERE TRANSACTIONNAME='" + txtVoucherName.Text.Trim() + "' and TypeId !=" + Request.QueryString["typeid"].ToString();
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            flag = false;
            lblMsg.Text = "Specified Voucher Name Alredy Exist";
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Specified Voucher Name Alredy Exist');", true);

        }
        strsql = "select ABBREVIATION FROM ACC_TRANSETTINGS WHERE ABBREVIATION='" + txtAbbreviation.Text.Trim() + "' and ABBREVIATION<>'' and TypeId !=" + Request.QueryString["typeid"].ToString();
        DataTable DT1 = DU.getdata(strsql);
        if (DT1.Rows.Count > 0)
        {
            flag = false;
            lblMsg.Text = "Specified ABBREVIATION Name Alredy Exist";
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Specified ABBREVIATION Name Alredy Exist');", true);

        }
        return flag;
    }

    protected void InsertTransactionMain(string typeid)
    {
        if (Validate())
        {
            string effectiveDate = string.Empty;
            string abbreviation = string.Empty;
            string billformatid = "0";

            if (txtEffectiveDate.Text.ToString() != string.Empty)
            {
                //  effectiveDate  = DateTime.Now.ToString("MM/dd/yyyy");
                string[] date1 = txtEffectiveDate.Text.ToString().Split('/');
                effectiveDate = date1[1].ToString() + "/" + date1[0].ToString() + "/" + date1[2].ToString();
            }

            if (txtAbbreviation.Text.ToString() != string.Empty)
            {
                abbreviation = txtAbbreviation.Text.Trim();
            }
            else
            {
                abbreviation = txtVoucherName.Text.Trim();
            }
            if (radioStandard.SelectedValue == "0")
            {
                billformatid = "0";
            }
            if (radioStandard.SelectedValue == "2")
            {
                billformatid = "2";
            }
            SqlParameter[] sparam =
                                        {
                                            new SqlParameter("@TYPEID",typeid),
                                            new SqlParameter("@PARENTTYPEID",ddlParentVoucher.SelectedValue),
                                            new SqlParameter("@TRANSACTIONNAME",txtVoucherName.Text.Trim()),
                                            new SqlParameter("@ISAUTONO",chkAutoNumber.Checked),
                                            new SqlParameter("@RESETNO",chkNewNumber.Checked),
                                            new SqlParameter("@ISREFERENCE",chkApplyReference.Checked),
                                            new SqlParameter("@ABBREVIATION",abbreviation),
                                            new SqlParameter("@ISDEFAULTNARRATION","0"),
                                            new SqlParameter("@EFFECTIVEDATE",effectiveDate),
                                            new SqlParameter("@BILLFORMATID",billformatid),
                                            new SqlParameter("@MODULETYPE","1"),
                                            new SqlParameter("@CCALLOCATIONTYPE","0")
                                        };
            int result = DU.executeprocedure("USP_ACC_SaveTransactionSetting", sparam);
            if (result > 0)
            {
                InsertTransactionDetails(typeid);
            }
        }

    }
    protected void InsertTransactionDetails(string typeid)
    {
        if (typeid.ToString() != string.Empty)
        {
            if (chkAutoNumber.Checked != true)
            {
                txtStartNumber.Text = "0";
            }
            SqlParameter[] sparam =
                                        {
                                            new SqlParameter("@TYPEID",typeid),
                                            new SqlParameter ("@FINYEAR",Session["FINYEAR"].ToString()),
                                            new SqlParameter("@STARTNO",txtStartNumber.Text.Trim()),
                                            new SqlParameter("@PREFIX",txtPrefix.Text.Trim()),
                                            new SqlParameter("@SUFFIX",txtSuffix.Text.Trim()),
                                            new SqlParameter("@ACCLEVELNARRATION","0"),
                                            new SqlParameter("@NARRATION",txtCommonNarration.Text.Trim()),
                                            new SqlParameter("@PRINTONSAVE",chkPrintAfterSave.Checked)
                                            
                                        };
            int result = DU.executeprocedure("USP_ACC_SaveTransactionSettingDetail", sparam);
            if (result > 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Updated');", true);
            }
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        InsertTransactionMain(Request.QueryString["typeid"].ToString());
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("VoucherFormat.aspx?action=view");
    }
}
